﻿#region License
// Copyright (c) 2012 Mund Consulting Ltd
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharePoint.Foundation.Publishing
{
    public partial class UrlPicker : LayoutsPageBase
    {
        const string constFolderIconPath = @"\_layouts\images\FOLDER.GIF";
        const string constDefaultIconPath = @"\_layouts\images\ICGEN.GIF";
        const string constImageFolderPath = @"\_layouts\images\";
        const string constOkScriptStart = "<script type=\"text/javascript\">window.frameElement.commonModalDialogClose(1,'";
        const string constOkScriptEnd = "');</script>";
        const string constCancelScript = "<script type=\"text/javascript\">window.frameElement.commonModalDialogClose();</script>";
        const string constTarget = "_self";
        const string constFormFolder = "Forms";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SPWeb curWeb = null;
                TreeNode node;
                curWeb = SPContext.Current.Web;
                tbxUrl.Text = curWeb.RootFolder.WelcomePage;
                node = new TreeNode(curWeb.Title, null, null, null, constTarget);
                trvLeft.Nodes.Add(node);
                TreeNode parentNode = node;
                foreach (SPList list in curWeb.Lists)
                {
                    if (!list.Hidden)
                    {
                        node = new TreeNode(list.Title, list.Title, list.ImageUrl, null, constTarget);
                        parentNode.ChildNodes.Add(node);
                    }
                }
            }
        }

        protected void ListSelected(object sender, EventArgs e)
        {
            trvMain.Nodes.Clear();
            SPWeb curWeb = null;
            TreeNode node;
            curWeb = SPContext.Current.Web;
            SPList lst = curWeb.Lists.TryGetList(trvLeft.SelectedValue);
            if (lst == null)
            {
                return;
            }
            SPFolder folder = lst.RootFolder;
            node = new TreeNode(lst.Title, lst.Title);
            LoadFolderNodes(folder, node);
            trvMain.Nodes.Add(node);
            node.Expand();
        }

       /// <summary>
        /// Traverse throuh all the files and folders in a given folder and load to the tree node
       /// </summary>
       /// <param name="folder"></param>
       /// <param name="folderNode"></param>
        private void LoadFolderNodes(SPFolder folder, TreeNode folderNode)
        {
            foreach (SPFolder childFolder in folder.SubFolders)
            {
                if (childFolder.Name != constFormFolder)
                {
                    TreeNode childFolderNode = new TreeNode(childFolder.Name, childFolder.Url, constFolderIconPath);
                    LoadFolderNodes(childFolder, childFolderNode);
                    folderNode.ChildNodes.Add(childFolderNode);
                }
            }
            foreach (SPFile file in folder.Files)
            {

                TreeNode fileNode;
                string iconUrl =string.Empty;
                if (string.IsNullOrEmpty(file.IconUrl))
                {
                    iconUrl = constDefaultIconPath;
                }
                else
                {
                    iconUrl = constImageFolderPath + file.IconUrl;
                }
                fileNode = new TreeNode(file.Name, file.Url, iconUrl);
                folderNode.ChildNodes.Add(fileNode);
            }
        }

        protected void NodeSelected(object sender, EventArgs e)
        {
        
            TreeNode node = trvMain.SelectedNode;
            tbxUrl.Text = node.Value;

        }

        protected void Ok(object sender, EventArgs e)
        {
            this.Page.Response.Clear();
            this.Page.Response.Write(constOkScriptStart + tbxUrl.Text + constOkScriptEnd);
            this.Page.Response.End();
        }
        protected void Cancel(object sender, EventArgs e)
        {
            this.Page.Response.Clear();
            this.Page.Response.Write(constCancelScript);
            this.Page.Response.End();
        }
    }
}
